package com.sskj.qiangjin.ui.activity;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.QRCodeUtil;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.RechargeBean;
import com.sskj.qiangjin.bean.RechargeInfoBean;
import com.sskj.qiangjin.presenter.RechargePresenter;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;

//@Route(path = RConfig.APP_RECHARGE)//充值
public class RechargeActivity extends BaseActivity<RechargePresenter> {

    @BindView(R.id.ivQRCode)
    ImageView ivQRCode;
    @BindView(R.id.tvSave)
    TextView tvSave;
    @BindView(R.id.tvCode)
    TextView tvCode;
    @BindView(R.id.tvCopy)
    TextView tvCopy;
    @BindView(R.id.tvDesc)
    TextView tvDesc;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    protected void initView() {
        setTitle("充值");
//        mPresenter.getRecharge();
    }

    public void updateView(RechargeBean bean) {
        tvCode.setText(bean.getUrl());
        if (TextUtils.isEmpty(bean.getQrc())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.app_get_again));
            return;
        }
        QRCodeUtil.createImage(bean.getQrc(), (int) RudenessScreenHelper.pt2px(this, 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                ivQRCode.setImageBitmap(bitmap);
            }

            @Override
            public void onAnalyzeFailed() {

            }
        });
        ClickUtil.click(tvCopy, () -> {
            CopyUtil.copy(tvCode.getText().toString());
        });
        ClickUtil.click(tvSave, () -> {
            new RxPermissions(RechargeActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            ImgUtil.saveImageToGallery(this, ivQRCode);
                        } else {
                            PermissionTipUtil.tip(RechargeActivity.this, App.INSTANCE.getString(R.string.app_permission_cache));
                        }
                    });
        });
    }
}
